package demo;

public class Test {
    public static Node 定义一条有4个元素的链表() {

        Node n1 = new Node(100);
        Node n2 = new Node(200);
        Node n3 = new Node(300);
        Node n4 = new Node(400);

        n1.next=n2;
        n2.next=n3;
        n3.next=n4;
        n4.next=null;
        Node head=n1;

        return head;
    }

    public static void 打印链表(Node head){
        for (Node cur=head;cur!=null;cur=cur.next){
            System.out.println(cur.val);
        }
    }

    public static int 统计一条链表的长度(Node head){
        int count=0;
        //遍历链表
        for (Node cur=head;cur!=null;cur=cur.next){
            count++;
        }
        return count;
    }

    public static void main(String[] args) {
        Node head=定义一条有4个元素的链表();
        System.out.println(head);//链表要打印需要遍历，这样只是打印了链表头结点对象
        打印链表(head);//遍历后打印链表
        System.out.println( 统计一条链表的长度( head));

    }
}